package com.ruoyi.visitor.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.visitor.config.VisitorConst;
import com.ruoyi.visitor.domain.VisitorAppointment;
import com.ruoyi.visitor.domain.VisitorWxUser;
import com.ruoyi.visitor.model.SessionInfo;
import com.ruoyi.visitor.model.UserInfoRequest;
import com.ruoyi.visitor.model.WxUserInfo;
import com.ruoyi.visitor.service.impl.VisitorAppService;
import com.ruoyi.visitor.service.impl.WechatLoginServiceImpl;
import com.ruoyi.visitor.util.QRCodeGenerator;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

/**
 * packageName com.ruoyi.visitor.controller
 *
 * @author wfx
 * @version JDK 8
 * @className AppController
 * @date 2025/4/12
 * @description 移动端
 */
@RestController
@RequestMapping("/app")
@CrossOrigin
public class VisitorAppController extends BaseController {

    @Autowired
    private WechatLoginServiceImpl wechatLoginService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private DictService dictService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private VisitorAppService appService;
    @RequestMapping("/getWxLoginUser")
    public SessionInfo getWxLoginUser(@RequestParam(required = false) String code){
        SessionInfo sessionInfo = wechatLoginService.getSessionInfo(code);
        return sessionInfo;
    }

    /**
     * 微信登录
     * @param wxUser
     * @return
     */
    @PostMapping("/wxLogin")
    public AjaxResult wxLogin(@RequestBody VisitorWxUser wxUser){
        try {
            int res = appService.wxLogin(wxUser);
            if (res > 0) {
                return AjaxResult.success("登录成功");
            }
            return AjaxResult.success("登录失败");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 创建预约
     * @param visitorAppointment
     * @return
     */
    @PostMapping("/createVisitorAppointment")
    public AjaxResult createVisitorAppointment(@RequestBody VisitorAppointment visitorAppointment){
        try {
            appService.insertVisitorAppointment(visitorAppointment);
            return AjaxResult.success("预约成功");
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 查询我的申请
     * @param visitorAppointment
     * @return
     */
    @PostMapping("/myAppointmentList")
    @ResponseBody
    public TableDataInfo myAppointmentList(VisitorAppointment visitorAppointment)
    {
        startPage();
        List<VisitorAppointment> list = appService.selectMyVisitorAppointmentList(visitorAppointment.getOpenid());
        return getDataTable(list);
    }

    /**
     * 查询我的审核
     * @param visitorAppointment
     * @return
     */
    @PostMapping("/myApprovalList")
    @ResponseBody
    public TableDataInfo myApprovalList(VisitorAppointment visitorAppointment)
    {
        startPage();
        List<VisitorAppointment> list = appService.selectMyApprovalList(visitorAppointment.getOpenid(),visitorAppointment);
        return getDataTable(list);
    }

    /**
     * 获取预约申请
     * @param id
     * @return
     */
    @GetMapping("/getVisitorAppointment")
    @ResponseBody
    public AjaxResult getVisitorAppointment(@RequestParam String id)
    {
        return AjaxResult.success(appService.selectVisitorAppointmentById(Long.valueOf(id)));
    }
    /**
     * 预约审核
     * @param appointmentId
     * @return
     */
    @PostMapping("/approval")
    @ResponseBody
    public AjaxResult approval(@RequestParam Long appointmentId,@RequestParam String approvalState){
        int res = appService.visitorApproval(appointmentId,approvalState);
        if(res > 0){
            return AjaxResult.success("审核成功");
        }else{
            return AjaxResult.error("审核失败");
        }
    }

    /**
     * 二维码生成
     * @param code
     * @return
     */
    @GetMapping(value = "/qrcode", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] generateQRCode(@RequestParam String code) {
        int width = 300;
        int height = 300;
        try {
            return QRCodeGenerator.generateQRCode(code, width, height);
        } catch (Exception e) {
            throw new RuntimeException("二维码生成失败: " + e.getMessage());
        }
    }

    /**
     * 访客码核验
     * @param code
     * @return
     */
    @PostMapping("/checkVisitorCode")
    public AjaxResult checkVisitorCode(String code,String openid){
        try {
            appService.checkvisitorCode(code, openid);
            return AjaxResult.success("核验通过");
        }catch (Exception e){
            return AjaxResult.success(e.getMessage());
        }
    }

    /**
     * 获取当前日期
     * @return
     */
    @PostMapping("/getCurrentDate")
    public AjaxResult getCurrentDate(){
        return AjaxResult.success("获取当前日期",DateUtil.today());
    }

    /**
     * 获取当前时间
     * @return
     */
    @PostMapping("/getCurrentTime")
    public AjaxResult getCurrentTime(){
        return AjaxResult.success("获取当前时间",DateUtil.format(new DateTime(),"HH:mm"));
    }


    /**
     * 获取通知
     * @return
     */
    @PostMapping("/getNotice")
    public AjaxResult getNotice(){
        SysNotice notice = noticeService.selectNoticeById(4L);
        return AjaxResult.success("获取告知书",notice.getNoticeContent());
    }

    /**
     * 来访事由
     * @return
     */
    @PostMapping("/getVisitorReason")
    public AjaxResult getVisitorReason(){
        List<SysDictData> list = dictService.getType(VisitorConst.DICT_VISITOR_RESION);
        return AjaxResult.success("来访事由",list);
    }

    /**
     * 访问部门
     * @return
     */
    @PostMapping("/getVisitedDept")
    public AjaxResult getVisitedDept(){
        SysDept dept = new SysDept();
        dept.setParentId(100L);
        List<SysDept> depts = deptService.selectDeptList(dept);
        return AjaxResult.success("访问部门",depts);
    }

}